window.onload = () =>{
    //---------------------------获取DOM---------------------------------
    var box = document.getElementById("box") ;
    var rdio1 = document.getElementById("rdio1");//获取跳跃小人元素
        rdio1.style.bottom = 102 +"px";
    var jifenBot = document.getElementById("jifenBot");//积分榜area显示
    // var jifenMid = document.getElementById("jifenMid");//当前总积分显示
    var dqjifen2 = document.getElementById("dqjifen2");//当前积分显示

    var BeginBtn = document.getElementById("BeginBtn");//获取开始按钮
    var StopBtn = document.getElementById("StopBtn");//获取开始按钮
    var BeginAgin = document.getElementById("BeginAgin");//获再来一局按钮
    
    var zuobixitong = document.getElementById("zuobixitong");
    zuobixitong.style.visibility = "hidden";
    // zuobixitong.style.visibility = "visible";
    //初始化变量
    var arrOneTimerSet = [] ;//保存每个定时器
    var arrOneTimerOut = [] ;//保存每个延时器

    var totalPoints = 0 ;//积分统计
    var oneStepPoint = 5 ;//每次多少积分
    var totalPintsArray = [] ;//保存所有总积分

    BeginAgin.disabled = true ;

    var fangzuobi = [] ;

    //---------------------------函数包装-----------------------------
    function huoquRdm(min,max){//获取范围内随机数
        return Math.random()*( max - min ) + min ;
    }

    var arr = [] ;//x,y,w,h---测试[1100,100,10,100]
    function huoquXY(){//在范围内，随机生成坐标和宽高        
        var x = huoquRdm( 0 , 190 ) + 1100 ;
        var y = 100 ;
        var w = 20 ;
        var h = huoquRdm( 20 , 100 ) ;//最低10px
        arr.push([x,y,w,h]) ;
    }
    // setInterval(function(){
    //     huoquXY();
    //     console.log(arr);
    // },500);
    var count = 0 ;//用于取出arr中的元素
   
    function oneGoing(){//一个div的创建和运动
        var timerLftGO = null ;//定义向左移动的时间控制器
        var countD = count ;
        count++ ;
        var div = document.createElement("div");
        div.style.left = arr[countD][0] + "px";//x
        div.style.bottom = arr[countD][1] + "px";//y
        div.style.width = arr[countD][2] + "px";//w
        div.style.height = arr[countD][3] + "px";//h
        div.style.background = "black" ;

        box.appendChild(div);

        timerLftGO = setInterval(function(){
            // div.style.left = parseFloat(div.style.left) - 45 + "px" ;
            // div.style.left = -1 + "px" ;
            
            if(parseFloat(div.style.left) < 0 ){
                clearInterval(timerLftGO);
                box.removeChild(div);
                // console.log(div.style.left);

            }
            div.style.left = parseFloat(div.style.left) - 45 + "px" ;
            // console.log(div.style.left);

        },150)
        arrOneTimerSet.push(timerLftGO);//将刷新木桩定时器存进数组

        var TimeOutCtrol = null ;
        TimeOutCtrol = (arr[countD][0] - 300 + 23 ) / (1000 / 150 * 45) * 1000;//木桩左边距离火柴人右边的路程----除以----每秒的速度   *   1000  转换毫秒
       
       
        var timerLftOut = setTimeout(function(){
            if((arr[countD][1] + arr[countD][3]) > parseFloat(rdio1.style.bottom) ){
                // console.log(arrOneTimerSet);
                // console.log(arrOneTimerOut);
                clearAllOneTimerSet();//清除所有的木桩定时器
                clearAllOneTimerOut();//清除所有的木桩延时器
                // console.log(arrOneTimerSet);
                // console.log(arrOneTimerOut);

                clearInterval(timerMore);//清除主函数
                window.alert("游戏结束");
                
                totalPintsArray.push(totalPoints);
                jifenBang();//积分榜显示
                clearPoints();//清除累加积分
                dqjifen2.innerHTML = totalPoints ;

                clearBoxChildren_shuzhuang();

                BeginAgin.disabled = false ;
                // console.log(arr[countD][1] + arr[countD][3],rdio1.height,TimeOutCtrol);
                // alert("结束游戏");
            }else{
                totalPoints += oneStepPoint ;//积分累加
                dqjifen2.innerHTML = totalPoints ;
            }
        },TimeOutCtrol);
        arrOneTimerOut.push(timerLftOut);//将刷新木桩延时器存进数组
    }

    function clearAllOneTimerSet(){//清除所有的木桩定时器
        for(var i = 0  ; i < arrOneTimerSet.length ; i++){
            clearInterval(arrOneTimerSet[i]);
        }
        arrOneTimerSet = [] ;
    }
    function clearAllOneTimerOut(){//清除所有的木桩延时器
        for(var i = 0  ; i < arrOneTimerOut.length ; i++){
            clearTimeout(arrOneTimerOut[i]);
        }
        arrOneTimerOut = [] ;
    }
 
    var timerMore = null ;//获取数组数据并创建运动时间控制
    function execMain(){//执行主函数
        timerMore = setInterval(function(){
            huoquXY();
           
            oneGoing();

            // console.log(arr);
        },1000);//300/距离*间隔秒数
    }
//---------------执行开始--------------------
    //execMain();
    
//---------------------跳跃事件绑定----------------------------
//  var rdio1 = document.getElementById("rdio1");

 document.onkeydown = function(e){
     var e = e || window.event ;
     if(e.keyCode == "32"){
         rdio1.style.bottom = "200px";
     }
     fangzuobi.length++;
     if(fangzuobi.length>=80){
         console.log("您已经开始作弊了");
         zuobixitong.style.visibility = "visible";
         setTimeout(function(){
            zuobixitong.style.visibility = "hidden";
         },3000)
     }
     if(fangzuobi.length>=150){
         window.alert("检测到您作弊，结束游戏");
         location.reload(true);//刷新清缓存
     }
 }
 document.onkeyup = function(e){
    fangzuobi.length = 0 ;
    console.log(fangzuobi.length);
    var e = e || window.event ;
    if(e.keyCode == "32"){
        rdio1.style.bottom = "102px";
    }
 }
 //积分统计函数
 function clearPoints(){//清除累加积分
    totalPoints = 0 ;
 }
  function jifenBang(){//积分榜显示赋值
    jifenBot.innerHTML="";
    totalPintsArray.sort(function(a,b){return b-a;});
    for(var i = 0 ; i < totalPintsArray.length ; i++ ){
        jifenBot.innerHTML += totalPintsArray[i] + "\n" ;
    }
      
  }

//按钮事件
BeginBtn.onclick = function(){//开始事件
    clearAllTimers();//清除所有计时器和延时器
    clearBoxChildren_shuzhuang();//清除所有的树桩

    execMain();//执行开始

    this.disabled=true;
    BeginAgin.disabled=false; 
   
    
}
StopBtn.onclick = function(){//暂停事件
    var a = confirm("继续吗？");
    if(!a){
        clearAllTimers();//清除所有计时器和延时器
        clearBoxChildren_shuzhuang();//清除所有的树桩
    }

    this.disabled=true;
    this.disabled=false; 
}

BeginAgin.onclick = function(){//再来一次按钮
    
    clearBoxChildren_shuzhuang();//清除所有的树桩
    
    this.disabled=true;
    // this.disabled=false; 

    execMain();//执行开始
    
}

function clearAllTimers(){//清除所有计时器和延时器
    fangzuobi.length = 0 ;
    clearAllOneTimerSet();//清除所有的木桩定时器
    clearAllOneTimerOut();//清除所有的木桩延时器
    clearInterval(timerMore);//清除主函数
}


function clearBoxChildren_shuzhuang(){//清除所有的树桩
    var boxChilds = [] ;
    for(var i = 6 ; i < box.children.length ; i++){
        boxChilds.push(box.children[i]);
    }
    
    for(var key in boxChilds) {
        box.removeChild(boxChilds[key]);
    }
}

function clearAlldate(){
    clearBoxChildren_shuzhuang();
}
















}